$(document).ready(function(){
    menuAccordion();
});

function encolheMenu(){
  $('.navbar-fixed menu').click(function() {
    $('.navbar-fixed menu').toggle('slow', function() {
    // Animation complete.
  });
});
}
function menuAccordion(){
    $('#accordion a.btn').click(function () {
        
        $(this).siblings('ul').slideDown('fast');
        if($(this).hasClass("over")){
            $(this).siblings('ul').slideUp('fast');
            $(this).removeClass("over");
        } else {
            $(this).addClass("over");
        }
        return false;
    });

}


function isEmpty(val){
    if($.isArray(val)){
        return val.length == 0;
    }else{
        return $.trim(val).length == 0.
    }
}

function isZero(val){
    return val ==0 || $.trim(val) =="";
}

function size(obj){
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
}

function dateIsValid(val){
    var reg = new RegExp(/^(((0[1-9]|[12][0-9]|3[01])[- /.](0[13578]|1[02])|(0[1-9]|[12][0-9]|30)[- /.](0[469]|11)|(0[1-9]|1\d|2[0-8])[- /.]02)[- /.]\d{4}|29[- /.]02[- /.](\d{2}(0[48]|[2468][048]|[13579][26])|([02468][048]|[1359][26])00))$/);
    return reg.test(val);
}

function validaCpf(val){
    var reg = new RegExp(/^\d{3}.\d{3}.\d{3}-\d{2}$/);
    return reg.test(val);
}

function validaCEP(value){
    return new RegExp(/^[0-9]{5}-[0-9]{3}$/).test(value);
}

function rolaTela(selector){
    $('html,body').animate({
        scrollTop: $(selector).offset().top
    }, 2000);
}

function exibeMsgErro(erros,id,idExibicao){
        
    idExibicao = preparaIdExibicao(idExibicao);
        
    if(erros != null && erros != undefined){
        removeMsgSucess(idExibicao);
            
        if($.isArray(erros) ){
            if(id == null & id != undefined){
                removeMsgErro(".error_class_"+id);
            }
                
            for(index in  erros){
                if( (id == null || id == undefined) && $(".error_class_"+index).length > 0){
                    removeMsgErro(".error_class_"+index);
                }
                    
                if(id == null || id == undefined){
                    $(idExibicao+" .alert-error ul").append('<li class="error_class_'+index+'">'+erros[index]+'</li>');
                }else{
                    $(idExibicao+" .alert-error ul").append('<li class="error_class_'+id+'">'+erros[index]+'</li>');
                        
                }
            }
        }else{
            $(idExibicao+" .alert-error ul").append('<li class="error_class_'+id+'">'+erros+'</li>');
        }
        $(idExibicao+" .alert-error").show();
            
        if(idExibicao == ""){
            $(".alert-error").stop();
            rolaTela('#rola');
        }else{
            $(idExibicao+" .alert-error").stop();
            rolaTela(idExibicao +' #rola');
            
        }
    }
}

function removeMsgErro(ids,idExibicao){
    idExibicao = preparaIdExibicao(idExibicao);
    
    if($.isArray(ids) && ids.length > 0){
        for(var index = 0; index <  ids.length; index++){
            $(".error_class_"+ids[index]).remove();
        }
    }else if($.trim(ids) == 0){
        $('.alert-error ul li').remove();
    }else{
        if($(".error_class_"+ids).length > 0 ){
            $(".error_class_"+ids).remove();
        }
    }
    if($(idExibicao+" .alert-error ul li").length == 0){
        escondeMsgErro(idExibicao);
    }
}
    
function escondeMsgErro(idExibicao ){
    idExibicao = preparaIdExibicao(idExibicao);
    $(idExibicao+" .alert-error").css("display","none");
    $(idExibicao+" .alert-error ul").html("");
}  

function removeMsgSucess(idExibicao){
    idExibicao = preparaIdExibicao(idExibicao);
    $(idExibicao+" .alert-error").css("display","none");
    $(idExibicao+" .alert-error ul").html("");
}

function exibeMsgSuccess(msg,idExibicao){
    idExibicao = preparaIdExibicao(idExibicao);
    
    removeMsgSucess(idExibicao);
    escondeMsgErro(idExibicao);
    $(idExibicao+".alert-error ul").append("<li>" + msg + "</li>");
    $(idExibicao+".alert-error").show();
}
function preparaIdExibicao(idExibicao){
    if(idExibicao == undefined || idExibicao == null || idExibicao == ""){
        idExibicao = ""
    }else if(idExibicao.indexOf("#") == -1 ){
        idExibicao = "#"+idExibicao+" ";
    }
    return idExibicao;
}